package com.innovate.nms;

import java.awt.Font;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.innovate.nms.view.MainFrame;
import com.ivan.toolkit.swing.Images;
import com.ivan.toolkit.swing.Splashor;

import lombok.Getter;
import lombok.extern.log4j.Log4j;

@SpringBootApplication
@EnableScheduling
@Log4j
public class NmsApplication {

	@Getter
	private static ConfigurableApplicationContext spring;

	public static void main(String[] args) {
		
		//SpringApplication.run(LeakviewApplication.class, args);
		
        try {
        	
        	UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        	
        	Font font = new Font("微软雅黑", Font.PLAIN, 12);
        	java.util.Enumeration<?> keys = UIManager.getDefaults().keys();
        	while (keys.hasMoreElements())
        	{
        		Object key = keys.nextElement();
        		Object value = UIManager.get(key);
        		if (value instanceof javax.swing.plaf.FontUIResource)
        		{
        			UIManager.put(key, font);
        		}
        	}
			
        } catch ( Exception e ) {
        	
            JOptionPane.showMessageDialog(
            		null, 
            		"设置界面风格失败", 
            		"错误", 
            		JOptionPane.INFORMATION_MESSAGE);
        }
		
        Splashor splashor = Splashor.create(getSplashorImage());
        splashor.start();
		
		SpringApplicationBuilder builder = new SpringApplicationBuilder(NmsApplication.class);
		
		spring = builder.headless(false).run(args);
		
		splashor.stop();
		
		try {
			MainFrame mf = spring.getBean(MainFrame.class);
			mf.setVisible(true);
		} catch(Exception e) {
			log.error("Show main frame failed.", e);
		}
		
	}
	
	static ImageIcon getSplashorImage() {
		
		File file = new File("splash.png");
		if(file.exists()) {
			return new ImageIcon("splash.png");
		}
		
		return Images.loadResource("/img/default_splash.png").getIcon();
	}
	
	
	public static void shutdown() {
		
		spring.close();
		
	}
}
